package com.chhuang.core.bean;

import com.chhuang.core.enums.SaCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @ClassName ChResult
 * @Description 封装前端请求返回结果
 * @Author Darren Huang
 * @Date 2022/11/19 13:03
 * @Version 1.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "封装前端请求返回结果对象", description = "封装前端请求返回结果，包括了code,msg和data")
public class SaResult<T> implements Serializable {
    private static final long serialVersionUID = -5258833270567888760L;

    @ApiModelProperty(value = "编码")
    private int code;
    @ApiModelProperty(value = "信息")
    private String msg;
    @ApiModelProperty(value = "数据")
    private T data;

    public static<T> SaResult<T> ok() {
        return new SaResult<T>(SaCode.SUCCESS.getCode(), SaCode.SUCCESS.getMessage(), (T)null);
    }

    public static<T> SaResult<T> ok(String msg) {
        return new SaResult<T>(SaCode.SUCCESS.getCode(), msg, (T)null);
    }

    public static<T> SaResult<T> code(int code) {
        return new SaResult<T>(code, (String)null, (T)null);
    }

    public static<T> SaResult<T> data(T data) {
        return new SaResult<T>(SaCode.SUCCESS.getCode(), SaCode.SUCCESS.getMessage(), data);
    }

    public static<T> SaResult<T> data(String msg, T data) {
        return new SaResult<T>(SaCode.SUCCESS.getCode(), msg, data);
    }

    public static<T> SaResult<T> error() {
        return new SaResult<T>(SaCode.INTERNAL_SERVER_ERROR.getCode(), SaCode.INTERNAL_SERVER_ERROR.getMessage(), (T)null);
    }

    public static<T> SaResult<T> error(String msg) {
        return new SaResult<T>(SaCode.INTERNAL_SERVER_ERROR.getCode(), msg, (T)null);
    }

    public static<T> SaResult<T> get(int code, String msg, T data) {
        return new SaResult<T>(code, msg, data);
    }

    public static<T> SaResult<T> get(int code, String msg) {
        return SaResult.get(code, msg, (T)null);
    }

    public String toString() {
        return "{\"code\": " + this.getCode() + ", \"msg\": " + this.getMsg() + ", \"data\": " + this.getData() + "}";
    }
}
